package com.android.androidbigwork.frames;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.android.androidbigwork.R;
import com.android.androidbigwork.adapter.Demo_List_Adapter;
import com.android.androidbigwork.entity.Item_Share;
import com.android.androidbigwork.pojo.LoginUser;
import com.android.androidbigwork.wedget.CircleImageView;
import com.android.androidbigwork.utils.MyCallBack;
import com.android.androidbigwork.utils.RequestUtils;
import com.bumptech.glide.Glide;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Demo extends Fragment {
    private static final String url =
            "http://39.108.183.17:8080/getShare";
    private static final String MyMessageUrl =
            "http://39.108.183.17:8080/getMyMessage?id="+ LoginUser.getAccount();
    private String baseUrl = "http://39.108.183.17:8080/images/";

    private CircleImageView circleImageView;
    private TextView name;
    private String[] mess = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.demo,container,false);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        circleImageView = getActivity().findViewById(R.id.headerP);
        name = getActivity().findViewById(R.id.demoTextView);

          Thread tread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String s = RequestUtils.get(MyMessageUrl);
//                    Log.d("", "run: "+s);
                    mess = s.split("&");
//                    Log.d("", "run: "+mess[0]+"\n"+mess[1]);
                    name.setText(mess[0]);

                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        });
          tread.start();

          while (true){
              if(!tread.isAlive()){
                  Glide.with(this.getContext()).load(baseUrl+mess[1]).into(circleImageView);
                  break;
              }
          }


        List<Item_Share> list = new ArrayList<>();
        Demo_List_Adapter demo_list_adapter =
                new Demo_List_Adapter(this.getContext(),R.layout.demo_item,list,this);

        MyCallBack myCallBack = new MyCallBack(this);
        myCallBack.setFragment(this);
        myCallBack.setDemo_list_adapter(demo_list_adapter);
        RequestUtils.flag = true;
        RequestUtils.getShares(url,myCallBack);


    }




    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}
